
$(function() {
	if( /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ) {
        //window.location = "http://b.shitangdingcan.com:8080/meals_mobile/#/intro";
		
		location.href = ctx + 'abutment/firstMobile';
    }
	
	 // 通过window.screen.width获取设备的宽度
    // var offWidth = window.screen.width;
    // 通过document.body.clientWidth网页可见区域的宽度
    var offWidth = document.body.clientWidth
    // 设计稿1rem = 100px
    document.getElementsByTagName("html")[0].style.fontSize = offWidth*(100/1920) + 'px'; //把rem的值复制给顶级标签html的font-size
    console.log('offWidth:'+offWidth)

    window.onresize = function(){
        location.reload()
    }

    validateRule();
    
    $(".i-checks").iCheck({checkboxClass:"icheckbox_square-green-login"});
	$('.imgcode').click(function() {
		var url = ctx + "captcha/captchaImage?type=" + captchaType + "&s=" + Math.random();
		$(".imgcode").attr("src", url);
	});
    
});

$.validator.setDefaults({
    submitHandler: function() {
		login();
    }
});

function login() {
	$.modal.loading($("#firstLoginSubmit").data("loading"));
	var companyAccount = $.common.trim($("input[name='companyAccount']").val());
	var loginName = $.common.trim($("input[name='loginName']").val());
    var loginPwd = $.common.trim($("input[name='loginPwd']").val());
    $.ajax({
        type: "post",
        url: ctx + "firstLogin",
        data: {
        	"companyAccount": companyAccount,
            "loginName": loginName,
            "loginPwd": loginPwd
        },
        success: function(r) {
            if (r.code == 0) {
            	var orgId = r.data.orgId;
            	var companyAccount = r.data.companyAccount;
            	var companyIp = r.data.companyIp;
            	var companyPort = r.data.companyPort;
            	var status = r.data.status;
    			
            	if(status == 1){
            		var firstURL = "http://"+companyIp+":"+companyPort+"/meals/firstRealLogin?loginName="+loginName+"&loginPwd="+loginPwd+"&orgId="+orgId+"&companyAccount="+companyAccount;
            		location.href = firstURL;
            	}else if(status == 0){
            		location.href = ctx + 'abutment/probation?companyIp='+companyIp+"&companyPort="+companyPort+"&loginName="+loginName+"&loginPwd="+loginPwd+"&orgId="+orgId+"&type=first"+"&companyAccount="+companyAccount;
            	}else{
            		location.href = ctx + 'abutment/errorpage';
            	}
            	
            } else {
            	$.modal.closeLoading();
            	$('.imgcode').click();
            	$.modal.msg(r.msg);
            }
        }
    });
}

function validateRule() {
    var icon = "<i class='fa fa-times-circle'></i> ";
    $("#firstLoginForm").validate({
        rules: {
        	companyAccount: {
                required: true
            },
            loginName: {
                required: true
            },
            loginPwd: {
                required: true
            }
        },
        messages: {
        	companyAccount: {
                required: "",
            },
            loginName: {
                required: "",
            },
            loginPwd: {
                required: "",
            }
        }
    })
}
